package com.example.mapper;


import com.example.pojo.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {
    @Insert("insert into article(title,content,cover_img,state,category_id,create_user,create_time,update_time) " +
            "values (#{title},#{content},#{coverImg},#{state},#{categoryId},#{createUser},#{createTime},#{updateTime}) ")
    void add(Article article);

    List<Article> list(Integer userId, Integer categoryId, String state);

    @Delete("delete from article where id = #{id} and create_user = #{userId}")
    void delete(Integer id, Integer userId);

    @Select("select * from article where id = #{id}")
    Article detail(Integer id);

    void update(Article article);

    @Select("select * from article")
    List<Article> blist();

    @Select("select article.id,article.title, article.cover_img, article.create_time,article.create_user, article.view_count,article.likes from article limit #{limit} ")
    List<Article> clist(Integer limit );

    List<Article> selectAll(Article article);

    @Select("select * from article where state = #{state}")
    List<Article> listByState(String state);

    @Update("update article set state = #{state} where id = #{id}")
    void updateState(Article article);

    List<Article> listAll(Integer userId, Integer categoryId, String state);

    @Select("select count(*) from `like` where article_id = #{articleId} and user_id = #{userid} and status = 1")
    Integer ifliked(Integer articleId, Integer userid);

    @Select("select count(*) from `favorite` where article_id = #{articleId} and user_id = #{userid}")
    Integer ifcollected(Integer articleId, Integer userid);

    @Select("select * from article where title like concat('%',#{keyword},'%') limit #{limit}")
    List<Article> searchByKeyword(String keyword, Integer limit);
//    让阅读量自增
    @Update("update article set view_count = view_count + 1 where id = #{id}")
    void addViewCount(Integer id);
}
